<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\rehearse\controller;

use app\common\controller\Admin;
use app\rehearse\controller\api\Record as RecordApi;
use app\rehearse\controller\api\Recorddetail as RecorddetailApi;
use app\admin\controller\ajax\Library as Ajax;


/**
 * 演练执行
 * @package app\plan
 */
class Record  extends Admin

{

    protected $api;

    public function _initialize()
    {
        parent::_initialize();
        $this->detail = new RecorddetailApi();
        $this->ajax= new Ajax();
        $this->api = new RecordApi();

    }
    /**
     * 首页
     *
     */
    public function index() {
        if(IS_AJAX){
            $recordList = $this->api->recordList();
            return $recordList;
        }

        return $this->fetch();
    }

    /**
     * 演练执行搜索
     */
    public function search($start=null,$end=null,$keyword=null) {
        if(IS_AJAX){
            $recordList = $this->api->search(input('start'),input('end'),input('keyword'));
            return $recordList;
        }
    }
    /**
     * 添加演练执行
     *
     */
    public function add() {
        if(IS_POST){
            $data = input('post.');
            return $this->api->add($data);
        }
        $eventList = model('duty/events')->column('title','id');

        $this->assign('eventList',$eventList);

        $planList = model('plan/plan')->column('title','id');

        $this->assign('planList',$planList);

        $userList=model('common/user')->column('userName','id');
        $this->assign('userList',$userList);
        return $this->fetch();
    }

    /**
     * 修改演练执行
     *
     */
    public function edit($recordId= null) {

        if(IS_POST){
            $data = input('post.');
            return $this->api->add($data);
        }
        $eventList = model('duty/events')->column('title','id');

        $this->assign('eventList',$eventList);

        $planList = model('plan/plan')->column('title','id');

        $this->assign('planList',$planList);

        $userList=model('common/user')->column('userName','id');
        $this->assign('userList',$userList);

        $recordList = $this->api->recordList($recordId)->getData();
        $this->assign('info',$recordList['data'][0]);
        return $this->fetch();
    }

    /**
     * 删除演练执行
     */
    public function del($recordId = null) {
        if(IS_AJAX){
            return $this->api->del($recordId);
        }
    }






    /**
     * 执行明细列表
     */
    // public function recorddetailList($recordId = null) {
    //     if(IS_AJAX){
    //     $recorddetailList = $this->recorddetail->recorddetailList($planId);
    //      return $recorddetailList;
    //   }
    //   $unitList = model('basic/unit')->column('fullName','id');
    //   $unitList[0]='管委会';
    //   $this->assign('unitList',$unitList);
    //   $this->assign('planId',$planId);
    //   return $this->fetch();
    //  }

    /**
     * 执行明细列表
     */
    public function recordDetailAdd($recordId = null,$eventId=null) {

        if(empty($recordId)){
            return 0;
        }
        if(IS_POST){
            $data = input('post.');
            $data['rescueExecId'] = $recordId;
            return $this->detail->add($data);
        }
        $type = $this->ajax->getScriptType()->getData();
        $this->assign('recordId',$recordId);
        $this->assign('eventId',$eventId);

        $eventList = model('duty/events')->column('title','id');
       
        $this->assign('eventList',$eventList);
        $this->assign('type',$type);

        $chargeperson = model('basic/unit')->column('chargePerson','id');
        $this->assign('chargeperson',$chargeperson);

        $status = $this->ajax->getimplementStatusType()->getData();
        $this->assign('status',$status);
        return $this->fetch();
    }


    /**
     * 修改执行明细
     *
     */
    public function recordDetailEdit($recordId = null,$eventId=null,$recorddetailId = null) {
        if(empty($recordId)){
            return 0;
        }
        if(IS_POST){
            $data = input('post.');
            $data['rescueExecId'] = $recordId;
            return $this->detail->edit($data,$recorddetailId);
        }
        $type = $this->ajax->getScriptType()->getData();
        $this->assign('recordId',$recordId);
        $this->assign('eventId',$eventId);
        
        $eventList = model('duty/events')->column('title','id');

        $this->assign('eventList',$eventList);
        $this->assign('type',$type);
        $status= $this->ajax->getimplementStatusType()->getData();
        $this->assign('status',$status);
        $chargeperson = model('basic/unit')->column('chargePerson','id');
        $this->assign('chargeperson',$chargeperson);
        $recorddetailList = $this->detail->recorddetailList($recordId,$recorddetailId)->getData();
        $this->assign('info',$recorddetailList['data'][0]);

        return $this->fetch();
    }




    /**
     * 删除执行明细
     */
    public function recorddetaildel($recorddetailId = null) {

        if(IS_AJAX){

            return $this->detail->del($recorddetailId);
        }
    }

    /**
     * 申请审核
     * @param string $id
     */
    public function audit($id = null){
        return $this->api->audit($id);
    }



}



